package com.slz.crm.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slz.crm.common.annotation.RequirePermission;
import com.slz.crm.common.enumeration.PermissionOperates;
import com.slz.crm.common.result.Result;
import com.slz.crm.pojo.dto.BusinessActivityDTO;
import com.slz.crm.pojo.dto.BusinessActivityQueryDTO;
import com.slz.crm.pojo.vo.BusinessActivityVO;
import com.slz.crm.server.service.BusinessActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业务活动
 */
@RestController
@RequestMapping("/business/activity")
public class BusinessActivityController {

    @Autowired
    private BusinessActivityService businessActivityService;

    /**
     * 创建活动
     * @param dto 活动DTO
     * @return 是否创建成功
     */
    @PostMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "CREATE_BUSINESS_ACTIVITY"
    )
    public Result<Boolean> create(@RequestBody BusinessActivityDTO dto) {
        return Result.success(businessActivityService.create(dto));
    }

    /**
     * 删除活动
     * @param idList 活动ID列表
     * @return 是否删除成功
     */
    @DeleteMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "DELETE_BUSINESS_ACTIVITY"
    )
    public Result<Integer> delete(@RequestBody List<Long> idList) {
        return Result.success(businessActivityService.deleteByIds(idList));
    }

    /**
     * 更新活动
     * @param businessActivityDTOList 活动DTO列表
     * @return 成功更新的数量
     */
    @PutMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "UPDATE_BUSINESS_ACTIVITY"
    )
    public Result<Integer> update(@RequestBody List<BusinessActivityDTO> businessActivityDTOList) {
        return Result.success(businessActivityService.updateList(businessActivityDTOList));
    }

    /**
     * 获取所有活动
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 活动列表
     */
    @GetMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "GET_ALL_BUSINESS_ACTIVITY"
    )
    private Result<List<BusinessActivityVO>> getAllActivity(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return Result.success(businessActivityService.getAllActivity(pageNum, pageSize));
    }

    /**
     * 自定义分页查询活动列表
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param dto 活动查询条件DTO
     * @return 活动列表
     */
    @PostMapping("/query")
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "BUSINESS_ACTIVITY_QUERY"
    )
    public Result<Page<BusinessActivityVO>> activityQuery(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody BusinessActivityQueryDTO dto) {
        Page<BusinessActivityVO> page = businessActivityService.businessActivityQuery(pageNum, pageSize, dto);
        return Result.success(page);
    }

    /**
     * 根据ID查询活动详情
     * @param id 活动ID
     * @return 活动详情
     */
    @GetMapping("/{id}")
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "GET_BUSINESS_ACTIVITY_DETAIL"
    )
    public Result<BusinessActivityVO> getById(@PathVariable Long id) {
        return Result.success(businessActivityService.getDetailById(id));
    }

}
